package com.superquan.selfstudybootmybatis.utils.encrypt;

import com.superquan.selfstudybootmybatis.consts.Constant;
import com.superquan.selfstudybootmybatis.utils.encrypt.base64.RSACoder;
import com.superquan.selfstudybootmybatis.utils.encrypt.md5.MD5Util;
import lombok.SneakyThrows;

/**
 * @program: self-study-bootmybatis
 * @ClassName EncryptTest
 * @description: 加密解密测试类
 * @author: zwq
 **/
public class EncryptTest {
    @SneakyThrows
    public static void main(String[] args) {
        EncryptTest.ppkOperate("superquan1754");
        EncryptTest.getSign("123456");
        EncryptTest.getEncryptMd5("1008611");
    }


    /**
     * 使用公钥加密私钥解密(Base64)
     * @param data 待加密的字符串
     * @throws Exception
     */
    public static void ppkOperate(String data) throws Exception {
        //加密后的用户名
        String encryptName="";
        //解密后的用户名
        String decryptName="";
        //使用公钥加密
        byte[] data1 = RSACoder.encryptByPublicKey(data, Constant.RSA.PUBLIC_KEY);
        encryptName = RSACoder.encryptBASE64(data1);
        System.out.println("加密后的用户名："+encryptName);
        //使用私钥解密
        byte[] data2 = RSACoder.decryptByPrivateKey(encryptName, Constant.RSA.PRIVATE_KEY);
        decryptName = new String(data2);
        System.out.println("解密后的用户："+decryptName);
    }

    /**
     * 用私钥对信息生成数字签名
     * @throws Exception
     */
    public static void getSign(String data) throws Exception {
        //使用公钥加密
        byte[] data1 = RSACoder.encryptByPublicKey(data, Constant.RSA.PUBLIC_KEY);
        //用私钥对信息生成数字签名
        String sign = RSACoder.sign(data1, Constant.RSA.PRIVATE_KEY);
        System.out.println("用私钥对信息生成数字签名为："+sign);
    }

    /**
     * 获取md5加密后的字符串
     * @param data
     */
    public static void getEncryptMd5(String data){
        String md5String = MD5Util.getMD5(data);
        System.out.println("md5加密后的字符串："+md5String);
    }
}
